﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Supermarket;

namespace XLSupermarket.Storehouse.Supplier_Information
{
    public partial class Supplier_Information : Form
    {
        public Supplier_Information()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 查询功能
        /// </summary>
        private void Select() {
            string sql = "select * from Supplier";
            dataGridView1.DataSource = DBHelper.FillData(sql);
        }
        /// <summary>
        /// 加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Supplier_Information_Load(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            Select();
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            string sql = "select * from Supplier where 1=1 ";
            if (TB_Name.Text!="")
            {
                sql += " and Supplier_Name like '%"+ TB_Name.Text + "%'";
            }
            if (TB_Addres.Text != "")
            {
                sql += " and Supplier_Address like '%"+ TB_Addres.Text + "%'";
            }
            if (TB_Peaple.Text != "")
            {
                sql += " and Supplier_Contact like '%"+ TB_Peaple.Text + "%'";
            }
            if (TB_Phone.Text != "")
            {
                sql += " and Supplier_Phone like '%"+ TB_Phone.Text + "%'";
            }
            dataGridView1.DataSource = DBHelper.FillData(sql);
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (TB_Name.Text == "" || TB_Addres.Text == ""|| TB_Peaple.Text == ""|| TB_Phone.Text == "")
            {
                MessageBox.Show("请将信息输入完整！");
                return;
            }
            string sql = string.Format("insert into Supplier Values('{0}','{1}','{2}','{3}')",
                TB_Name.Text, TB_Peaple.Text, TB_Phone.Text, TB_Addres.Text);
            try
            {
                int num = DBHelper.Edit(sql);
                if (num>0) MessageBox.Show("添加成功！");
                Select();
            }
            catch (Exception ex) {
                MessageBox.Show("您已添加该供货商，无需重复添加！");
            }
            
        }
        /// <summary>
        /// 单机数据表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            string index = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            string sql = "select * from Supplier where Supplier_ID='"+index+"'";
            DataTable dt = DBHelper.FillData(sql);
            LB_ID.Text = dt.Rows[0][0].ToString();
            TB_Name.Text = dt.Rows[0][1].ToString();
            TB_Peaple.Text = dt.Rows[0][2].ToString();
            TB_Phone.Text = dt.Rows[0][3].ToString();
            TB_Addres.Text = dt.Rows[0][4].ToString();
        }
        /// <summary>
        /// 清空功能
        /// </summary>
        private void Clear() {
            LB_ID.Text = "";
            TB_Name.Text = "";
            TB_Peaple.Text = "";
            TB_Phone.Text = "";
            TB_Addres.Text = "";
        }
        /// <summary>
        /// 清空按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            Clear();
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            if (TB_Name.Text == "" || TB_Addres.Text == "" || TB_Peaple.Text == "" || TB_Phone.Text == "")
            {
                MessageBox.Show("请将信息输入完整！");
                return;
            }
            string sql = string.Format("update Supplier set Supplier_Name='{0}',Supplier_Contact='{1}',Supplier_Phone='{2}',Supplier_Address='{3}' where Supplier_ID='{4}'",
                TB_Name.Text, TB_Peaple.Text, TB_Phone.Text, TB_Addres.Text,LB_ID.Text);
            int num = DBHelper.Edit(sql);
            if (num > 0) MessageBox.Show("修改成功！");
            else MessageBox.Show("修改失败！");
            Select();
        }
    }
}
